import {
  Body,
  Controller,
  Get,
  HttpException,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import { AuthService } from '../services/auth.service';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { AuthorBaseControll } from './authorBase.controller';
import { AuthGuard } from '../guards/authority.guard';
import {
  AddAuthDto,
  DeleteAuth,
  RoleAuthDto,
  UpdateAuthDto,
} from '../dtos/auth.dto';

@ApiTags('权限中心')
@Controller('/auth')
@UseGuards(AuthGuard)
export class AuthController extends AuthorBaseControll {
  constructor(private authService: AuthService) {
    super();
  }

  @ApiOperation({ summary: '获取权限列表' })
  @Get('/getAuthList')
  getAuthListController() {
    return this.authService.getAuthListService();
  }

  @ApiOperation({ summary: '获取角色对应的权限' })
  @Get('/getAuthListByRoleId')
  getAuthByRoleIdControll(@Query('roleId') roleId: string) {
    return this.authService.getAuthByRoleIdServe(roleId);
  }

  @ApiOperation({ summary: '修改角色权限' })
  @Post('/updateAuthRoleId')
  updateAuthRoleIdControll(@Body() RoleAuth: RoleAuthDto) {
    return this.authService.updateAuthRoleIdServe(RoleAuth);
  }

  @ApiOperation({ summary: '修改权限详情' })
  @Post('/updateAuthCon')
  async updateAuthConControll(@Body() UpdateAuthCon: UpdateAuthDto) {
    const result = await this.authService.updateAuthConServe(UpdateAuthCon);
    if (result.affectedRows === 1) {
      return {
        message: '修改成功',
      };
    } else {
      throw new HttpException('修改失败', 401);
    }
  }

  @ApiOperation({ summary: '新增二级菜单' })
  @Post('/addChildAuthCon')
  async addChildAuthControll(@Body() AddAuthCon: AddAuthDto) {
    const result = await this.authService.addChildAuthServe(AddAuthCon);
    if (result.affectedRows === 1) {
      return {
        message: '新增成功',
      };
    } else {
      throw new HttpException('新增失败', 401);
    }
  }

  @ApiOperation({ summary: '删除权限' })
  @Post('/deleteAuthCon')
  async deleteAuthControll(@Body() DeleteAuth: DeleteAuth) {
    const result = await this.authService.deleteAuthServe(DeleteAuth);
    if (result.affectedRows === 1) {
      return {
        message: '删除成功',
      };
    } else {
      throw new HttpException('删除失败', 401);
    }
  }
}
